package com.xbongbong.paas.help.service.impl;

import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.help.base.BaseLinkBoolQueryBuilderService;
import com.xbongbong.paas.help.service.BoolQueryBuilderService;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.pojo.dto.LinkSearchListDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.paas.util.ParentBusinessConditionUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * @author: wufeng
 * @date: 2018/11/7 20:00
 * @desrcption: 关联查询
 */
@Component
public class LinkSearchBoolQueryBuilderServiceImpl extends BaseLinkBoolQueryBuilderService implements BoolQueryBuilderService<LinkSearchListDTO> {

    private static  final Logger LOG = LoggerFactory.getLogger(LinkSearchBoolQueryBuilderServiceImpl.class);

    @Resource
    private PaasFormExplainModel paasFormExplainModel;

    @Override
    public Boolean getBoolQueryBuilder( BoolQueryBuilder boolQueryBuilder, LinkSearchListDTO linkSearchListDTO) throws XbbException{
        try {
            String corpid = linkSearchListDTO.getCorpid();
            LinkInfoPojo linkInfoPojo = linkSearchListDTO.getLinkInfo();
            if(linkInfoPojo == null || linkInfoPojo.getLinkFormId() == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.NOT_SET_LINK_FORM);
            }

            Long linkFormId = linkInfoPojo.getLinkFormId();
            // 搜索当前表单字段信息
            PaasFormExplainEntity paasFormExplainEntity = paasFormExplainModel.getByFormId(linkSearchListDTO.getFormId(), corpid);
            if(paasFormExplainEntity == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
            }
            Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMap(paasFormExplainEntity.getExplains(), null);
            // 搜索联动表单字段
            PaasFormExplainEntity relyExplain = paasFormExplainModel.getByFormId(linkFormId, corpid);
            if(relyExplain == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_EXPLAIN_NOT_EXIST);
            }
            // 联动表单字段容器
            Map<String, FieldAttrEntity> relyExplainMap = ExplainUtil.getExplainMap(relyExplain.getExplains(), null);
            // 获取值容器
            Map<String, Object> valueMap = getValueMap(explainMap, linkSearchListDTO.getValue(), linkInfoPojo, corpid);
            // 获取对应的es条件
            Boolean result = returnBoolQueryBuilder(relyExplainMap, explainMap, valueMap, linkInfoPojo.getCondition(), boolQueryBuilder, linkInfoPojo, corpid);
            if (!result) {
                return result;
            }
            //解析查询条件
            boolQueryBuilder.filter(termQuery("corpid.keyword", corpid));
            boolQueryBuilder.filter(termQuery("formId", linkInfoPojo.getLinkFormId()));
            boolQueryBuilder.filter(termQuery("del", 0));
            ParentBusinessConditionUtil.productCondition(boolQueryBuilder, linkInfoPojo.getLinkBusinessType(), linkInfoPojo.getLinkBusinessType(), null);

        }catch (XbbException e) {
            throw e;
        } catch (Exception e){
            LOG.error("linkSearchBoolQueryBuilder获取es搜索条件失败", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return true;
    }
}
